package com.hyg.module_report.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hyg.module_report.R;

import java.util.ArrayList;

import io.reactivex.rxjava3.annotations.NonNull;


/**
 * Created by panfu on 2020-04-18.
 */

public class AnalysisResultAdapter extends RecyclerView.Adapter<AnalysisResultAdapter.MyHolder> {

    private ArrayList<String> tongue_name = new ArrayList<>();
    private ArrayList<String> tongue_analysis = new ArrayList<>();
    private Context activity;

    public AnalysisResultAdapter(Context context, ArrayList<String> tongue_name, ArrayList<String> tongue_analysis) {
        this.activity = context;
        this.tongue_name.clear();
        this.tongue_analysis.clear();
        this.tongue_name.addAll(tongue_name);
        this.tongue_analysis.addAll(tongue_analysis);
    }

    @NonNull
    @Override
    public AnalysisResultAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int ViewType) {
        View view = LayoutInflater.from(activity).inflate(
                R.layout.item_tongue_analysis, parent, false);
        AnalysisResultAdapter.MyHolder holder = new AnalysisResultAdapter.MyHolder(view);
        Log.d("checkId", "onCreateViewHolder");
        return holder;
    }



    @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, final int position) {
//        holder.rg_respondent.setOnCheckedChangeListener(null);
        //重点，取消监听在初始化RadioGroup之前，我们要先取消RadioGroup的监听，因为我们在初始化的时候，不管是调用setCheck(true)还是check(R.id.radioButton)来初始化选择，
        // 都会被checkChangeListener监听到，从而使没有数据的item也获得数据
        // 如果该位置的值为空，则不要初始化，要调用clearCheck()方法来清空，这样就避免了来回滑动，item复用的原因导致的check错乱！
        Log.d("checkId", "onBindViewHolder: 验证是否重用了");

        holder.tv_tongue_name.setText(tongue_name.get(position));
        holder.tv_tongue_analysis.setText("\u3000\u3000" + tongue_analysis.get(position));

    }


    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (tongue_name == null) {
            return 0;
        } else {
            return tongue_name.size();
        }
    }


    class MyHolder extends RecyclerView.ViewHolder {
        private TextView tv_tongue_name;
        private TextView tv_tongue_analysis;

        public MyHolder(View itemView) {
            super(itemView);
            tv_tongue_name = (TextView) itemView.findViewById(R.id.tv_tongue_name);
            tv_tongue_analysis = (TextView) itemView.findViewById(R.id.tv_tongue_analysis);

        }

    }


}

